from sqlalchemy.orm import Session

from app.models.credit_transaction import CreditTransaction
from app.schemas.credit_transaction import CreditTransactionCreate

def create_transaction(db: Session, *, transaction_in: CreditTransactionCreate) -> CreditTransaction:
    db_obj = CreditTransaction(**transaction_in.model_dump())
    db.add(db_obj)
    db.commit()
    db.refresh(db_obj)
    return db_obj

def get_transactions_by_user(db: Session, *, user_id: int, skip: int = 0, limit: int = 100) -> list[CreditTransaction]:
    return (
        db.query(CreditTransaction)
        .filter(CreditTransaction.user_id == user_id)
        .order_by(CreditTransaction.created_at.desc())
        .offset(skip)
        .limit(limit)
        .all()
    )
